package com.ruoyi.yl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.yl.domain.YlDrug;
import com.ruoyi.yl.domain.YlStockAndOutInfo;
import com.ruoyi.yl.domain.YlStockManageInfo;
import com.ruoyi.yl.mapper.DrugMapper;
import com.ruoyi.yl.mapper.StockControlMapper;
import com.ruoyi.yl.service.StockControlService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: linKe
 * @Date: 2022/2/19 9:29
 * @FileName: StockControlServiceImpl
 * @version: 1.0
 * @Description: 库存管理
 */
@Service
@Transactional(readOnly = true)
public class StockControlServiceImpl implements StockControlService {
    @Resource
    private StockControlMapper stockControlMapper;

    @Resource
    private DrugMapper drugMapper;
    /**
     * 查询仓库中所有药品信息
     */
    @Override
    public List<YlDrug> getAllDrug() {
        return stockControlMapper.selectList(null);
    }

    /**
     * 根据药品的处方类型查药品
     */
    @Override
    public List<YlDrug> getDrugsByType(Long id) {
        return stockControlMapper.selectList(new QueryWrapper<YlDrug>().eq("drug_type",id));
    }

    /**
     * 根据药品名称、编码、生产厂家进行模糊查询
     */
    @Override
    public List<YlDrug> getDrugsByLikes(String dsc) {
        return stockControlMapper.getDrugsByLikes(dsc);
    }

    /**
     * 根据药品id查询出入库和药品信息
     */
    @Override
    public YlStockManageInfo getInfoById(Long id) {
        // 全部出库
        List<YlStockAndOutInfo> allOutDrugById = stockControlMapper.getAllOutDrugById(id);
        // 全部入库
        List<YlStockAndOutInfo> allStockDrugById = stockControlMapper.getAllStockDrugById(id);
        // 查询药品信息
        YlDrug drug = drugMapper.selectById(id);

        // 合并
        allOutDrugById.addAll(allStockDrugById);
        return new YlStockManageInfo().setDrug(drug).setList(allOutDrugById);
    }
}
